package com.groupscore.user.controller;

import com.github.promeg.pinyinhelper.Pinyin;
import com.groupscore.user.pojo.User;
import com.groupscore.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.*;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private HttpServletRequest request;
    /**
     * 查询全部列表
     */
    @GetMapping("/findAll")
    @CrossOrigin
    public Result findAll(String token){

        Map map = new HashMap(  );


        Claims claims = jwtUtil.parseJWT(token);
        if (claims != null) {
            return new Result(true,StatusCode.OK,"查询成功122",userService.findAll());
        }
        return new Result(false, StatusCode.ACCESSERROR,"无权访问");
    }

    /**
     * 增加用户
     */
    @PostMapping("/add")
    @CrossOrigin
    public Result add(@RequestBody User user, String token){

        Claims claims = jwtUtil.parseJWT(token);
        if (claims != null) {
            //判断是管理员
            if("admin".equals( claims.get( "roles" ) )){
                //名字转拼音(小写),作为账号
                user.setName( Pinyin.toPinyin( user.getNikename() , "" ).toLowerCase() );
                user.setPassword( Pinyin.toPinyin( user.getNikename() , "" ).toLowerCase());

                //判断没带小组id设置成其他
                if(user.getGroupId() == null){
                    user.setGroupId( "5" );
                }

                user.setCreateTime( new Date() );

                userService.add(user);
                return new Result(true,StatusCode.OK,"添加成功",null);
            }
        }
        return new Result(false, StatusCode.ACCESSERROR,"无权访问");
    }
    // 过期时间，单位毫秒
    private static final long EXPIRE_TIME = 7*24*3600*1000;
    /**
     * 用户登陆
     * @param u
     * @return
     */
    @RequestMapping(value="/login",method=RequestMethod.POST)
    @CrossOrigin
    public Result login(@RequestBody User u){

        User user = userService.findByNameAndPassword(u.getName(),u.getPassword());
        if(user!=null){
            String token = null;
            if(u.getName().equals( "renguoliang" )){
                token = jwtUtil.createJWT(user.getId(),user.getName(),"admin");
            }else{
                token = jwtUtil.createJWT(user.getId(),user.getName(),"user");
            }
            Map map = new HashMap();
            map.put("token", token);
            map.put("name", user.getName());
            map.put("nikename", user.getNikename());
            map.put("id",user.getId());
            return new Result(true,StatusCode.OK,"登陆成功", map);
        }else{
            return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误");
        }
    }

    @GetMapping("/info")
    @CrossOrigin
    public Result info(String token){
        Claims claims = jwtUtil.parseJWT(token);
        if (claims != null) {
            return new Result(true,StatusCode.OK,"返回消息",userService.findUserById(claims.get("jti")+""));
        }
        return new Result(false, StatusCode.ACCESSERROR,"无权访问");
    }
    @GetMapping("/logout")
    @CrossOrigin
    public Result logout(){
        return new Result(true, StatusCode.OK, "退出成功");
    }

    @GetMapping("/findNameById/{id}")
    public Result findNameById(@PathVariable("id") String id ){
        String name = userService.findNameById( id );
        return new Result( true, StatusCode.OK , "查询成功", name);
    }
}
